import { ResouceConst } from "../../../../component/gconst/ResouceConst";
import { ImagesConst } from "../../../resource/ImagesConst";
import { ChristmasListBody } from "./ChristmasListBody";
import { GBoxDirection } from "../../../../../com/mui/containers/globalVariable/GBoxDirection";
import { GScrollPane } from "../../../../../com/mui/controls/GScrollPane";
import { ScrollPolicy } from "../../../../../fl/controls/ScrollPolicy";
import { ChristmasItem } from "./ChristmasItem";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { ObjectPool } from "../../../../../com/gengine/utils/pools/ObjectPool";
import { ScaleBitmap } from "../../../../../com/mui/display/ScaleBitmap";
type int = number;
//class ChristmasList
    
    export  class ChristmasList /* flash.display.Sprite */
    {
        constructor(arg1: int, arg2: int)
        {
            
            this._widht = arg1;
            this._height = arg2;
            this.initUI();
            return;
        }

        /* internal  */initUI(): void
        {
            this._bg = ResouceConst.getScaleBitmap(ImagesConst.WindowCenterB);
            this._bg.width = this._widht;
            this._bg.height = this._height;
            addChild(this._bg);
            this._bodySprite = new ChristmasListBody();
            this._bodySprite.direction = GBoxDirection.VERTICAL;
            this._bodySprite.addEventListener(flash.events.MouseEvent.CLICK, this.onListClickHandler);
            this._scrollPane = new GScrollPane();
            this._scrollPane.x = 2;
            this._scrollPane.y = 2;
            this._scrollPane.width = this._widht - 4;
            this._scrollPane.height = this._height - 4;
            this._scrollPane.styleName = "GScrollPane";
            this._scrollPane.verticalScrollPolicy = ScrollPolicy.AUTO;
            this._scrollPane.horizontalScrollPolicy = ScrollPolicy.OFF;
            this._scrollPane.source = this._bodySprite;
            this._scrollPane.scrollDrag = false;
            this._scrollPane.focusEnabled = false;
            addChild(this._scrollPane);
            return;
        }

        /* internal  */onListClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.target as Object /* flash.display.DisplayObject */;
            if (loc1 && loc1 is ChristmasItem) 
            {
                this.changeSelected(loc1 as ChristmasItem);
            }
            return;
        }

        /* internal  */changeSelected(arg1: ChristmasItem): void
        {
            if (this._lastItem) 
            {
                if (this._lastItem.info.code != arg1.info.code) 
                {
                    this._lastItem.selected(false);
                    arg1.selected(true);
                    this._lastItem = arg1;
                    dispatchEvent(new DataEvent(EventName.ChristmasListSelectedChange, this._lastItem.info));
                }
            }
            else 
            {
                arg1.selected(true);
                this._lastItem = arg1;
                dispatchEvent(new DataEvent(EventName.ChristmasListSelectedChange, this._lastItem.info));
            }
            return;
        }

        public updateData(arg1: Array<any>): void
        {
            var loc1=0;
            var loc3=null;
            var loc4=null;
            this.dispose();
            var loc2=arg1.length;
            while (loc1 < loc2) 
            {
                loc3 = arg1[loc1];
                (loc4 = ObjectPool.getObject(ChristmasItem)).updateData(loc3);
                this._bodySprite.addChild(loc4);
                ++loc1;
            }
            this._bodySprite.invalidate();
            this._scrollPane.refreshPane();
            this._scrollPane.update();
            this.refreshData();
            return;
        }

        protected refreshData(): void
        {
            var loc1=null;
            if (this._bodySprite.numChildren > 0) 
            {
                loc1 = this._bodySprite.getChildAt(0) as ChristmasItem;
                this.changeSelected(loc1);
                this._scrollPane.verticalScrollPosition = 0;
                this._scrollPane.update();
            }
            return;
        }

        public dispose(): void
        {
            var loc1=null;
            while (this._bodySprite.numChildren > 0) 
            {
                loc1 = this._bodySprite.removeChildAt(0) as ChristmasItem;
                loc1.dispose();
                ObjectPool.disposeObject(loc1, ChristmasItem);
            }
            if (this._lastItem) 
            {
                this._lastItem.selected(false);
                this._lastItem.dispose();
                this._lastItem = null;
            }
            return;
        }

        public /* override */ get width(): number
        {
            return this._widht;
        }

        public /* override */ get height(): number
        {
            return this._height;
        }

        /* internal */ /* var */_bg: ScaleBitmap;

        /* internal */ /* var */_widht: int;

        /* internal */ /* var */_height: int;

        /* internal */ /* var */_bodySprite: ChristmasListBody;

        /* internal */ /* var */_scrollPane: GScrollPane;

        /* internal */ /* var */_lastItem: ChristmasItem;
    }
